package postgresql

import (
	"fmt"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

type Config struct {
	Host     string // 域名
	Port     int    // 端口
	User     string // 用户名
	Password string // 密码
	DbName   string //数据库名
}

var db *gorm.DB

func Get() *gorm.DB {
	return db
}
func Init(config *Config) error {
	dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
		config.Host,
		config.Port,
		config.User,
		config.Password,
		config.DbName,
	)
	var err error
	db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		return err
	} else {
		fmt.Println("postgresql config load success")
		return nil
	}
}

/*
type User struct {
	ID         int
	Type       int
	CreateTime time.Time
	UpdateTime time.Time
}

func Test() {
	var result = User{}
	db.Table("zhy_usr").First(&result)
	fmt.Println(result)
}
*/
